package com.wzz.controller;

import com.wzz.dto.AddUserDto;
import com.wzz.entity.Notice;
import com.wzz.entity.ReqExam;
import com.wzz.entity.User;
import com.wzz.entity.UserRole;
import com.wzz.service.NoticeService;
import com.wzz.service.ReqExamService;
import com.wzz.service.UserRoleService;
import com.wzz.service.UserService;
import com.wzz.utils.CertificateUtil.DateTimeUtil;
import com.wzz.utils.JwtUtils;
import com.wzz.vo.CommonResult;
import com.wzz.vo.PageResponse;
import com.wzz.vo.TokenVo;
import com.wzz.vo.UserInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Date;
import java.util.List;


@Validated
@RestController
@RequiredArgsConstructor
@Api(tags = "请求信息相关的接口")
@RequestMapping(value = "/reqExam")
public class ReqExamController {
    private final ReqExamService reqExamService;
    private final UserService userService;


    @PostMapping("/addReqExam")
    @ApiOperation("添加请求信息")
    public CommonResult<Void> addUser(@RequestBody @Valid ReqExam reqExam,HttpServletRequest request) {
        TokenVo tokenVo = JwtUtils.getUserInfoByToken(request);
        String username = tokenVo.getUsername();
        User user = userService.getUserByUsername(username);
        reqExam.setCreatedate(DateTimeUtil.DateToString(new Date()));
        reqExam.setFlag("1");
        reqExam.setUserid(user.getId());
        reqExam.setUsername(user.getTrueName());
        reqExamService.addReqExam(reqExam);
        return CommonResult.<Void>builder().build();
    }


    @PostMapping("/getAllReqExam")
    @ApiOperation("获取请求数据列表)")
    @ApiImplicitParams({})
    public CommonResult<List<ReqExam>> getAllReqExam() {
        return CommonResult.<List<ReqExam>>builder()
                .data(reqExamService.getReqExamples())
                .build();
    }


    @PostMapping("/updateReqExam")
    @ApiOperation("更新请求状态")
    public CommonResult<Void> updateReqExam(@RequestBody @Valid ReqExam reqExam) {
        reqExam.setFlag("2");
        reqExamService.updateById(reqExam);
        return CommonResult.<Void>builder().build();
    }
}
